package com.woniuxy.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.admin.exception.PawnShopException;
import com.woniuxy.admin.exception.PawnWarehouseException;
import com.woniuxy.admin.dto.PawnWarehouseDto;
import com.woniuxy.admin.entity.PawnShop;
import com.woniuxy.admin.entity.PawnShopWarehouse;
import com.woniuxy.admin.entity.PawnWarehouse;
import com.woniuxy.admin.form.InsertPawnWarehouseForm;
import com.woniuxy.admin.form.UpdatePawnWarehouseForm;
import com.woniuxy.admin.mapper.PawnShopMapper;
import com.woniuxy.admin.mapper.PawnShopWarehouseMapper;
import com.woniuxy.admin.mapper.PawnWarehouseMapper;
import com.woniuxy.admin.service.PawnWarehouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ...
 * @since 2024年05月22日
 */
@Service
public class PawnWarehouseServiceImpl extends ServiceImpl<PawnWarehouseMapper, PawnWarehouse> implements PawnWarehouseService {

    @Resource
    private PawnShopMapper pawnShopMapper;

    @Resource
    private PawnShopWarehouseMapper pawnShopWarehouseMapper;

    /**
     * 分页查询所有仓库
     * @param pageNum 分页页数
     * @param pageSize 每页个数
     * @param condition 条件查询
     * @return
     */
    @Override
    public Page<PawnWarehouseDto> loadAllPawnWarehourse(Integer pageNum, Integer pageSize,String condition) {
        Page<PawnWarehouse>page=new Page<>(pageNum,pageSize);
        Page<PawnWarehouseDto> pages = new Page<>();
        List<PawnWarehouseDto> dtos=new ArrayList<>();
        QueryWrapper<PawnWarehouse> wrapper1 = new QueryWrapper<>();
        if (condition != null && condition != "") {
        wrapper1.like("wh_name",condition).or();
        wrapper1.like("contact",condition).or();
        wrapper1.like("wh_phone",condition).or();
        wrapper1.like("address",condition);
        }
        this.page(page,wrapper1);
        if(page.getRecords()!=null&&page.getRecords().size()>0){
            for (PawnWarehouse pawnWarehouse : page.getRecords()) {
                PawnWarehouseDto dto = BeanUtil.toBean(pawnWarehouse, PawnWarehouseDto.class);
            QueryWrapper<PawnShopWarehouse> wrapper=new QueryWrapper<>();
            wrapper.eq("warehouse_id",dto.getWarehouseId());
                List<PawnShopWarehouse> pawnShopWarehouses = pawnShopWarehouseMapper.selectList(wrapper);
                if(pawnShopWarehouses!=null&&pawnShopWarehouses.size()>0){
                    for (PawnShopWarehouse pawnShopWarehouse : pawnShopWarehouses) {
                        PawnShop pawnShop = pawnShopMapper.selectById(pawnShopWarehouse.getShopId());
                        dto.getPawnShops().add(pawnShop);
                    }
                }
                dtos.add(dto);
            }
            pages.setRecords(dtos);
            pages.setPages(page.getPages());
            pages.setSize(page.getSize());
            pages.setCurrent(page.getCurrent());
            pages.setTotal(page.getTotal());
        }
        return pages;
    }

    /**
     * 新增仓库
     * @param form 新增仓库参数
     */
    @Override
    public void insertPawnWarehouse(InsertPawnWarehouseForm form) {
        List<PawnWarehouse> list = this.list();
        if(list!=null&&list.size()>0){
            for (PawnWarehouse pawnWarehouse : list) {
                if(pawnWarehouse.getWhName().equals(form.getWhName())){
                    throw new PawnWarehouseException(40001,"仓库名重复");
                }
            }
        }
        PawnWarehouse pawnWarehouse = BeanUtil.toBean(form, PawnWarehouse.class);
        pawnWarehouse.setState(0);
        this.save(pawnWarehouse);
    }

    /**
     * 更新仓库信息
     * @param form 更新仓库参数
     */
    @Override
    public void updatePawnWarehouse(UpdatePawnWarehouseForm form) {
        PawnWarehouse pawnWarehouse = this.getById(form.getWarehouseId());
        if(pawnWarehouse==null){
            throw  new PawnWarehouseException(40002,"仓库不存在");
        }
        pawnWarehouse.setAddress(form.getAddress());
        pawnWarehouse.setContact(form.getContact());
        pawnWarehouse.setWhName(form.getWhName());
        pawnWarehouse.setWhPhone(form.getWhPhone());
        this.updateById(pawnWarehouse);
    }

    /**
     * 更新仓库状态
     * @param warehouseId 仓库id
     * @param state 状态码
     */
    @Override
    public void updatePawnWarehouseState(Integer warehouseId, Integer state) {
        PawnWarehouse pawnWarehouse = this.getById(warehouseId);
        if(pawnWarehouse==null){
            throw  new PawnWarehouseException(40002,"仓库不存在");
        }
        if(state!=0&&state!=1) throw new PawnShopException(10002,"状态有误");
        pawnWarehouse.setState(state);
        this.updateById(pawnWarehouse);
    }

    /**
     * 分页查询所有仓库
     *
     * @param pageNum  分页页数
     * @param pageSize 每页个数
     * @param shopId 门店ID
     * @return
     */
    @Override
    public Page<PawnWarehouse> findNoBingdingWhByShopId(Integer pageNum, Integer pageSize, Integer shopId) {
        List<PawnShopWarehouse> pawnShopWarehouses = pawnShopWarehouseMapper.selectList(new QueryWrapper<PawnShopWarehouse>().eq("shop_id", shopId));
        Page<PawnWarehouse>page=new Page<>(pageNum,pageSize);
        QueryWrapper<PawnWarehouse>wrapper=new QueryWrapper<>();
        if(pawnShopWarehouses!=null&&pawnShopWarehouses.size()>0){
            List<Integer> whIds = pawnShopWarehouses.stream().map(pw -> pw.getWarehouseId()).collect(Collectors.toList());
            wrapper.notIn("warehouse_id",whIds);
        }
        this.page(page,wrapper);
        return page;
    }
}
